<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 后台登录模块
// +----------------------------------------------------------------------
namespace app\base\controller;

use think\captcha\Captcha;
use think\facade\Config;
use think\Controller;
use app\base\model\Admin;
use Session;

class Login extends Controller
{
  public function index()
  {
    if(Session::has('admin') == false) {
      if($this->request->isPost()) {
        $data = input('post.');
        if(!captcha_check($data['captcha'])){
          return ['code' => 0, 'msg' => '验证码错误'];
        };
        $admin = new Admin();
        $return = $admin->login($data);
        return ['code' => $return['code'], 'msg' => $return['msg']];
        addLog();
      } else {
        return $this->fetch();
      }
    } else {
      $this->redirect('base/index/index');
    }
  }

  /**
   * 管理员退出，清除名字为admin的session
   * @return [type] [description]
   */
  public function logout()
  {
    Session::delete('admin');
    Session::delete('group_id');
    if(Session::has('admin') or Session::has('group_id')) {
      return $this->error('退出失败');
    } else {
      return $this->success('正在退出...','base/login/index');
    }
  }

  //验证码
  public function verify(){
    $captcha = new Captcha(Config::get('app.verify'));
    return $captcha->entry();
  }
}

?>